<?php
class RecoverAction extends PublicAction {
    //回收站首页
	public function index(){
		$this->display();
	}
    
	//图片回收站
	public function picture(){
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		import("ORG.Util.Page");
		$picture = D('PictureView');
		//if(strtolower($_SERVER['REQUEST_METHOD']) == 'post'){
			$key = $_GET['searkey'];
			if($key && $key != '请输入搜索关键字'){
				$map['_string'] = "(`code` = '".$key."') or (`title` like '%".$key."%')";
				$this->assign('search','1');
				$this->assign('key',$key);
			}
		//}
		$map['is_dele'] = 1;
		$count = $picture->where($map)->count();
		$page = new Page($count,15);
		$list = $picture->where($map)->limit($page->firstRow.",".$page->listRows)->order('uploadtime desc')->select();
		$this->assign("page",$page->show());
		$this->assign('list',$list);
		$this->display(); 
	}
	
	//挂图回收站
	public function image(){
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		import("ORG.Util.Page");
		$image = D('ImageView');
		//if(strtolower($_SERVER['REQUEST_METHOD']) == 'post'){
			$key = $_GET['searkey'];
			if($key && $key != '请输入搜索关键字'){
				$map['_string'] = "(`code` = '".$key."') or (`title` like '%".$key."%')";
				$this->assign('search','1');
				$this->assign('key',$key);
			}
		//}
		$map['is_dele'] = 1;
		$count = $image->where($map)->count();
		$page = new Page($count,15);
		$list = $image->where($map)->limit($page->firstRow.",".$page->listRows)->order('uploadtime desc')->select();
		$this->assign("page",$page->show());
		$this->assign('list',$list);
		$this->display(); 
	}
	
	//视频回收站
	public function video(){
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		import("ORG.Util.Page");
		$video = D('VideoView');
		//if(strtolower($_SERVER['REQUEST_METHOD']) == 'post'){
			$key = $_GET['searkey'];
			if($key && $key != '请输入搜索关键字'){
				$map['_string'] = "(`code` = '".$key."') or (`title` like '%".$key."%')";
				$this->assign('search','1');
				$this->assign('key',$key);
			}
		//}
		$map['is_dele'] = 1;
		$count = $video->where($map)->count();
		$page = new Page($count,15);
		$list = $video->where($map)->limit($page->firstRow.",".$page->listRows)->order('uploadtime desc')->select();
		$this->assign("page",$page->show());
		$this->assign('list',$list);
		$this->display(); 
	}
	
	//视频回收站
	public function article(){
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		import("ORG.Util.Page");
		$article = D('ArticleView');
		//if(strtolower($_SERVER['REQUEST_METHOD']) == 'post'){
			$key = $_GET['searkey'];
			if($key && $key != '请输入搜索关键字'){
				$map['_string'] = "(`code` = '".$key."') or (`title` like '%".$key."%')";
				$this->assign('search','1');
				$this->assign('key',$key);
			}
		//}
		$map['is_dele'] = 1;
		$count = $article->where($map)->count();
		$page = new Page($count,15);
		$list = $article->where($map)->limit($page->firstRow.",".$page->listRows)->order('uploadtime desc')->select();
		$this->assign("page",$page->show());
		$this->assign('list',$list);
		$this->display(); 
	}
	
	//图书回收站
	public function book(){
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		import("ORG.Util.Page");
		$book = D('BookView');
		//if(strtolower($_SERVER['REQUEST_METHOD']) == 'post'){
			$key = $_GET['searkey'];
			if($key && $key != '请输入搜索关键字'){
				$map['_string'] = "(`code` = '".$key."') or (`title` like '%".$key."%')";
				$this->assign('search','1');
				$this->assign('key',$key);
			}
		//}
		$map['is_dele'] = 1;
		$count = $book->where($map)->count();
		$page = new Page($count,15);
		$list = $book->where($map)->limit($page->firstRow.",".$page->listRows)->order('uploadtime desc')->select();
		$this->assign("page",$page->show());
		$this->assign('list',$list);
		$this->display(); 
	}
	
	//动漫回收站
	public function flash(){
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		import("ORG.Util.Page");
		$flash = D('FlashView');
		//if(strtolower($_SERVER['REQUEST_METHOD']) == 'post'){
			$key = $_GET['searkey'];
			if($key && $key != '请输入搜索关键字'){
				$map['_string'] = "(`code` = '".$key."') or (`title` like '%".$key."%')";
				$this->assign('search','1');
				$this->assign('key',$key);
			}
		//}
		$map['is_dele'] = 1;
		$count = $flash->where($map)->count();
		$page = new Page($count,15);
		$list = $flash->where($map)->limit($page->firstRow.",".$page->listRows)->order('uploadtime desc')->select();
		$this->assign("page",$page->show());
		$this->assign('list',$list);
		$this->display(); 
	}
	
	//游戏回收站
	public function game(){
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		import("ORG.Util.Page");
		$game = D('GameView');
		//if(strtolower($_SERVER['REQUEST_METHOD']) == 'post'){
			$key = $_GET['searkey'];
			if($key && $key != '请输入搜索关键字'){
				$map['_string'] = "(`code` = '".$key."') or (`title` like '%".$key."%')";
				$this->assign('search','1');
				$this->assign('key',$key);
			}
		//}
		$map['is_dele'] = 1;
		$count = $game->where($map)->count();
		$page = new Page($count,15);
		$list = $game->where($map)->limit($page->firstRow.",".$page->listRows)->order('uploadtime desc')->select();
		$this->assign("page",$page->show());
		$this->assign('list',$list);
		$this->display(); 
	}
	
	//展品回收站
	public function exhibits(){
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		import("ORG.Util.Page");
		$exhibits = D('ExhibitsView');
		//if(strtolower($_SERVER['REQUEST_METHOD']) == 'post'){
			$key = $_GET['searkey'];
			if($key && $key != '请输入搜索关键字'){
				$map['_string'] = "(`code` = '".$key."') or (`title` like '%".$key."%')";
				$this->assign('search','1');
				$this->assign('key',$key);
			}
		//}
		$map['is_dele'] = 1;
		$count = $exhibits->where($map)->count();
		$page = new Page($count,15);
		$list = $exhibits->where($map)->limit($page->firstRow.",".$page->listRows)->order('uploadtime desc')->select();
		$this->assign("page",$page->show());
		$this->assign('list',$list);
		$this->display(); 
	}
	
	//知识题回收站
	public function knowledge(){
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		import("ORG.Util.Page");
		$knowledge = D('KnowledgeView');
		//if(strtolower($_SERVER['REQUEST_METHOD']) == 'post'){
			$key = $_GET['searkey'];
			if($key && $key != '请输入搜索关键字'){
				$map['_string'] = "(`code` = '".$key."') or (`title` like '%".$key."%')";
				$this->assign('search','1');
				$this->assign('key',$key);
			}
		//}
		$map['is_dele'] = 1;
		$count = $knowledge->where($map)->count();
		$page = new Page($count,15);
		$list = $knowledge->where($map)->limit($page->firstRow.",".$page->listRows)->order('uploadtime desc')->select();
		$this->assign("page",$page->show());
		$this->assign('list',$list);
		$this->display(); 
	}
	
	//删除资源
	public function dele(){
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		$pid = $_POST['pid'];
		$model = $_POST['model'];
		$key = $_POST['key'];
		if($pid && $model && $key)
		{
			$map[$key] = $pid;
			$list = M($model)->field('url')->where($map)->find();
			if($list){
				$pic = explode(',', $list['url']);
				foreach($pic as $keys => $value){
					unlink('./Public/Uploads/'.$model.'/'.$value);
	        	 	unlink('./Public/Uploads/'.$model.'/m_'.$value);
	        	 	unlink('./Public/Uploads/'.$model.'/s_'.$value);
	        	 	unlink('./Public/Uploads/'.$model.'/sy_'.$value);
				}
			}
			$res = M($model)->where($map)->delete();
			if($res) echo 1;
			else echo 0;
		}
		else echo 0;
	}
	
	//删除资源
	public function delet(){
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		$pid = $_POST['pid'];
		$model = $_POST['model'];
		$key = $_POST['key'];
		if($pid && $model && $key)
		{
			$map[$key] = $pid;
			$list = M($model)->field('url,imageurl')->where($map)->find();
			if($list['url']){
				unlink('./Public/Uploads/'.$model.'/'.$list['url']);
			}
			if($list['imageurl']){
				unlink('./Public/Uploads/'.$model.'/Image/'.$list['imageurl']);
				unlink('./Public/Uploads/'.$model.'/Image/m_'.$list['imageurl']);
			}
			$res = M($model)->where($map)->delete();
			if($res) echo 1;
			else echo 0;
		}
		else echo 0;
	}
	
	//还原资源
	public function recover(){
		$isLogin = session(C('ADMIN_AUTH_KEY'));
		if(!$isLogin) $this->redirect('User/login');
		$pid = $_POST['pid'];
		$model = $_POST['model'];
		$key = $_POST['key'];
		if($pid && $model && $key)
		{
			$map[$key] = $pid;
			$data['is_dele'] = 0;
			$res = M($model)->where($map)->save($data);
			if($res) echo 1;
			else echo 0;
		}

		else echo 0;
		
	}
}